package com.example.tys.baseapplication.Permission;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;

/**
 * @author tys
 * @date 2019/4/19
 */

public class PermissionsFragment extends android.support.v4.app.Fragment {

    private static final int PERMISSIONS_REQUEST_CODE = 42;
    private static final String TAG = "PermissionsFragment";
    private PermissionHelper.PermissionsCallback permissionsCallback;
    private PermissionHelper.SetAfterCallback afterSettingCallback;

    public PermissionsFragment() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setRetainInstance(true);
    }


    @Override
    public void onDestroy() {
        permissionsCallback = null;
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult:" + requestCode);
        if (afterSettingCallback != null) {
            afterSettingCallback.onAppSetAfter(requestCode);
        }
    }

    /**
     * 申请权限
     *
     * @param permissions {@link Manifest.permission}
     * @param requestCode 请求码
     */
    @RequiresApi(Build.VERSION_CODES.M)
    protected void requestPermissions(@NonNull String permissions, @Nullable Integer requestCode) {
        super.requestPermissions(new String[]{permissions}, requestCode == null ? PERMISSIONS_REQUEST_CODE : requestCode);
    }

    @Override
    @TargetApi(Build.VERSION_CODES.M)
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        String permission = permissions[0];
        onRequestPermissionsResult(requestCode, permission, grantResults[0],
                shouldShowRequestPermissionRationale(permissions[0]));
    }

    private void onRequestPermissionsResult(int requestCode, String permission, int grantResult, boolean shouldShowRequestPermissionRationale) {
        Log.d(TAG, "requestCode:" + requestCode + " permission:" + permission + " grantResult:" + grantResult +
                " shouldShowRequestPermissionRationale:" + shouldShowRequestPermissionRationale);
        if (permissionsCallback != null) {
            permissionsCallback.onRequestPermissionsResult(requestCode, permission, grantResult, shouldShowRequestPermissionRationale);
        }
    }

    void setPermissionsCallback(PermissionHelper.PermissionsCallback permissionsCallback) {
        this.permissionsCallback = permissionsCallback;
    }

    public void setAfterSettingCallback(PermissionHelper.SetAfterCallback afterSettingCallback) {
        this.afterSettingCallback = afterSettingCallback;
    }
}
